package com.linxi.handy.component.menu;

import com.linxi.handy.Application;
import com.linxi.handy.component.control.HomeControl;
import com.linxi.handy.component.dialog.DatabaseDialog;
import com.linxi.handy.component.dialog.ExecMysqlDialog;
import com.linxi.handy.component.dialog.SetNullDialog;
import com.linxi.handy.component.dialog.TableDialog;
import com.linxi.handy.component.listener.DatabaseList;
import com.linxi.handy.component.view.Tableview;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 菜单
 *
 * @author user
 */
public class TopMenu extends JMenuBar {

    JMenu create = new JMenu("新 建     ");
    JMenu execute = new JMenu("执 行     ");
    JMenu config = new JMenu("设 置     ");

    JMenuItem createDb = new JMenuItem(" + 建 库 ");
    JMenuItem createTable = new JMenuItem(" + 建 表 ");
    JMenuItem createNewConnect = new JMenuItem(" + 创 建 新 连 接 ");
    JMenuItem executeMatter = new JMenuItem(" 执 行 事 务 ");
    JMenuItem exportExcel = new JMenuItem(" 导 出 Excel ");
    JMenuItem ExcelImport = new JMenuItem(" Excel 导 入 ");
    JMenuItem setNullValue = new JMenuItem(" 修 改 空 值 表 示 ");
    JMenuItem resetView = new JMenuItem(" 重 置 页 面 和 设 置 ");

    public TopMenu() {
        createDb.addActionListener(new ItemProcessListener());
        createTable.addActionListener(new ItemProcessListener());
        createNewConnect.addActionListener(new ItemProcessListener());
        create.add(createDb);
        create.add(createTable);
        create.add(createNewConnect);

        executeMatter.addActionListener(new ItemProcessListener());
        execute.add(executeMatter);

        setNullValue.addActionListener(new ItemProcessListener());
        resetView.addActionListener(new ItemProcessListener());
        config.add(setNullValue);
        config.add(resetView);

        this.add(create);
        this.add(execute);
        this.add(config);
    }

    class ItemProcessListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == createDb) {
                new DatabaseDialog(HomeControl.base);
                return;
            }
            if (e.getSource() == createTable) {
                new TableDialog(HomeControl.base);
                return;
            }
            if (e.getSource() == executeMatter) {
                new ExecMysqlDialog(HomeControl.base);
                return;
            }
            if (e.getSource() == setNullValue) {
                new SetNullDialog(HomeControl.base);
                return;
            }
            if (e.getSource() == resetView) {
                DatabaseList.addTree();
                HomeControl.ft.removeAll();
                HomeControl.ft.add(HomeControl.ft.mainTextArea, "Welcome");
                HomeControl.fdi.textPane.setText(null);
                Tableview.control = "Null(1)";
            }
            if (e.getSource() == createNewConnect) {
                new Application();
                HomeControl.base.dispose();
            }
        }
    }
}
